package com.toubutu.audio.admin.controller;

import com.toubutu.anno.LoginRequired;
import com.toubutu.audio.admin.dto.BatchDeleteDto;
import com.toubutu.audio.admin.dto.CategoryDto;
import com.toubutu.audio.admin.vo.VideoCategoryTreeVo;
import com.toubutu.audio.common.PermissionCode;
import com.toubutu.audio.entity.VideoCategory;
import com.toubutu.audio.service.VideoCategoryService;
import com.toubutu.audio.valid.ValidGroup;
import com.toubutu.common.Result;
import com.toubutu.permission.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 戒躁
 * @since 2024-01-10
 */
@RestController
@RequestMapping("/admin/category")
@Api(tags = "后台视频分类接口")
@LoginRequired
public class AdminVideoCategoryController {

    @Resource
    private VideoCategoryService categoryService;

    @GetMapping("/tree")
    @ApiOperation("获取视频分类树形结构")
    @Permission(value = PermissionCode.Category.CATEGORY_LIST)
    public Result<List<VideoCategoryTreeVo>> getCategoryTree(){
        return Result.ok(categoryService.getVideoCategoryTree());
    }

    @PostMapping("/add")
    @ApiOperation("添加分类")
    @Permission(value = PermissionCode.Category.CATEGORY_ADD)
    public Result<Void> addCategory(@RequestBody @Validated(value = ValidGroup.Add.class)  CategoryDto categoryDto){
        categoryService.addCategory(categoryDto);
        return Result.ok();
    }
    @PostMapping("/delete")
    @ApiOperation("删除分类")
    @Permission(value = PermissionCode.Category.CATEGORY_DELETE)
    public Result<Void> deleteCategoryList(@RequestBody @Valid BatchDeleteDto<Integer> batchDeleteDto){
        categoryService.batchDeleteCategory(batchDeleteDto.getDeleteIds());
        return Result.ok();
    }
    @PostMapping("/update")
    @ApiOperation("修改分类")
    @Permission(value = PermissionCode.Category.CATEGORY_UPDATE)
    public Result<Void> updateCategory(@RequestBody @Validated(value = ValidGroup.Update.class)  CategoryDto categoryDto){
        categoryService.updateCategoryById(categoryDto);
        return Result.ok();
    }

    @GetMapping("/info/{categoryId}")
    @ApiOperation("获取视频分类信息")
    @Permission(value = PermissionCode.Category.CATEGORY_DETAIL)
    public Result<VideoCategory> getInfo(@PathVariable Integer categoryId){
        return Result.ok(categoryService.getVideoCategoryInfo(categoryId));
    }
}
